/**********************************************************************
* Filename: CartaoCredito.java
* Created: 2013/05/31
* @author Fernando Brito e Abreu
**********************************************************************/
package org.quasar.airNova.businessLayer;

import org.quasar.airNova.persistenceLayer.Database;

import java.util.Set;
import java.util.HashSet;

public class CartaoCredito
{
	
	/***********************************************************
	* @return all instances of class CartaoCredito
	***********************************************************/
	public static Set<CartaoCredito> allInstances()
	{
		return Database.allInstances(CartaoCredito.class);
	}
	
	private Cliente possuidor;
	private CalendarDate data_expira;
	private int numero;
	
	/**********************************************************************
	* Default constructor
	**********************************************************************/
	public CartaoCredito()
	{
	}
	
	/**********************************************************************
	* Parameterized constructor
	* @param possuidor the possuidor to initialize
	* @param data_expira the data_expira to initialize
	* @param numero the numero to initialize
	**********************************************************************/
	public CartaoCredito(Cliente possuidor, CalendarDate data_expira, int numero)
	{
		this.possuidor = possuidor;
		this.data_expira = data_expira;
		this.numero = numero;
	}
	
	/**********************************************************************
	* ONE2MANY getter for CartaoCredito[*] <-> Cliente[1]
	* @return the possuidor of the cartaoCredito
	**********************************************************************/
	public Cliente possuidor()
	{
		return possuidor;
	}
	
	/**********************************************************************
	* ONE2MANY setter for CartaoCredito[*] <-> Cliente[1]
	* @param possuidor the possuidor to set
	**********************************************************************/
	public void setPossuidor(Cliente possuidor)
	{
		this.possuidor = possuidor;
	}
	
	/**********************************************************************
	* Standard attribute getter
	* @return the data_expira of the cartaoCredito
	**********************************************************************/
	public CalendarDate data_expira()
	{
		return data_expira;
	}
	
	/**********************************************************************
	* Standard attribute setter
	* @param data_expira the data_expira to set
	**********************************************************************/
	public void setData_expira(CalendarDate data_expira)
	{
		this.data_expira = data_expira;
	}
	
	/**********************************************************************
	* Standard attribute getter
	* @return the numero of the cartaoCredito
	**********************************************************************/
	public int numero()
	{
		return numero;
	}
	
	/**********************************************************************
	* Standard attribute setter
	* @param numero the numero to set
	**********************************************************************/
	public void setNumero(int numero)
	{
		this.numero = numero;
	}
	
	/**********************************************************************
	* ONE2MANY getter for CartaoCredito[0..1] <-> Reserva[*]
	* @return the reservas of the cartaoCredito
	**********************************************************************/
	public Set<Reserva> reservas()
	{
		Set<Reserva> result = new HashSet<Reserva>();
		for (Reserva x : Reserva.allInstances())
			if (x.cartaoCredito()  ==  this)
				result.add(x);
		return result;
	}
	
	/**********************************************************************
	* ONE2MANY multiple setter for CartaoCredito[0..1] <-> Reserva[*]
	* @param reservas the reservas to set
	**********************************************************************/
	public void setReservas(Set<Reserva> reservas)
	{
		for (Reserva x : reservas)
			x.setCartaoCredito(this);
	}
	
	/**********************************************************************
	* ONE2MANY single setter for CartaoCredito[0..1] <-> Reserva[*]
	* @param reserva the reserva to add
	**********************************************************************/
	public void addReservas(Reserva reserva)
	{
		reserva.setCartaoCredito(this);
	}
	
	/**********************************************************************
	* Object serializer
	**********************************************************************/
	public String toString()
	{
		return "CartaoCredito [possuidor=" + possuidor + ", data_expira=" + data_expira + ", numero=" + numero + "]";
	}
	
	//	-------------------------------------------------------------------------------
	//	INVARIANTS (TODO)
	/*
	inv cartaoValido
		self.reservas->collectNested($e : Reserva | $e.bilhetes)->flatten->collectNested($e : Bilhete | $e.realizacao)->collectNested($e : Realizacao | $e.ocorre_em)->forAll(d : CalendarDate | self.data_expira.isAfter(d))
	
	*/
}
